<?php

include FCPATH . 'application/controllers/backend/backend.php';

class Member extends Backend {

    public function __construct() {
	parent::__construct();

	$this->load->model('member_model', 'model');
    }

    public function index() {
        $this->load_javascripts('backend_member');
	$this->rend('backend/member_index_view');
    }

    public function records() {
	$page = intval($this->input->get('page'));
	$page_size = intval($this->input->get('pageSize'));
	$offset = ($page - 1) * $page_size;

	$this->json_back('XT001', '', array(
	    'total' => $this->model->total(),
	    'records' => $this->model->records($page_size, $offset)
	));
    }
    
    public function find(){
        $id = intval($this->uri->segment(4));
        
        $data = $this->model->find($id);
        
        $this->json_back('XT001','',$data);
    }

    public function insert() {
	$data = array(
	    'name' => trim($this->input->post('name')),
            'name_en' => trim($this->input->post('name_en')),
            'link' => trim($this->input->post('link'))
	);

	if ($this->model->insert($data)) {
	    $this->json_back('XT001','添加成功');
	} else {
	    $this->json_back('XT002', 'database error.');
	}
    }

    public function update(){
        $id = intval($this->input->post('id'));
        
        $data = array(
	    'name' => trim($this->input->post('name')),
            'name_en' => trim($this->input->post('name_en')),
            'link' => trim($this->input->post('link'))
	);

	if ($this->model->update($id,$data)) {
	    $this->json_back('XT001','更新成功');
	} else {
	    $this->json_back('XT002', 'database error.');
	}
    }
    
    public function delete() {
	$id = intval($this->uri->segment(4));

	if ($this->model->delete($id)) {
	    $this->json_back('XT001','删除成功');
	} else {
	    $this->json_back('XT002', 'database error.');
	}
    }

}